package com.ucl.web.config;

import com.ucl.config.MyProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StopWatch;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private final Logger LOG = LoggerFactory.getLogger(SwaggerConfig.class);

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public Docket createRestApi(MyProperties properties) {

        LOG.debug("Starting Swagger");
        StopWatch watch = new StopWatch();
        watch.start();

        Contact contact = new Contact(
                properties.getSwagger().getContactName(),
                properties.getSwagger().getContactUrl(),
                properties.getSwagger().getContactEmail());

        List<VendorExtension> vendorExtensions = new ArrayList();
        vendorExtensions.add(new StringVendorExtension("x-default-value", "默认的token"));

        ApiInfo apiInfo = new ApiInfo(
                properties.getSwagger().getTitle(),
                properties.getSwagger().getDescription(),
                properties.getSwagger().getVersion(),
                properties.getSwagger().getTermsOfServiceUrl(),
                contact,
                properties.getSwagger().getLicense(),
                properties.getSwagger().getLicenseUrl(),
                vendorExtensions);
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                //选择controller包
                //.apis(RequestHandlerSelectors.basePackage("com.ucl.web.controller")) //两者写法均可
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();

        watch.stop();
        LOG.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
        return docket;
    }


    /*@Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //选择controller包
                //.apis(RequestHandlerSelectors.basePackage("com.ucl.web.controller")) //两者写法均可
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //自定义信息可按需求填写
                .title("Spring Boot中使用Swagger构建RESTful APIs")
                .description("方便api测试")
                .termsOfServiceUrl("http://www.springio.com")
                .contact("ucl")
                .version("0.0.1")
                .build();
    }*/

}
